import {
  NestInterceptor,
  ExecutionContext,
  Injectable,
  CallHandler,
} from '@nestjs/common';
// import { classToPlain } from 'class-transformer';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

// @Injectable()
// export class TransformInterceptor implements NestInterceptor {
//   intercept(context: ExecutionContext, next: CallHandler<any>) {
//     console.log(next.handle().pipe(map((data) => classToPlain(data))));

//     return next.handle().pipe(map((data) => classToPlain(data)));
//   }
// }
interface Response<T> {
  data: T;
}

@Injectable()
export class TransformInterceptor<T>
  implements NestInterceptor<T, Response<T>>
{
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map((data) => {
        // console.log('===拦截器===', data);
        return {
          data,
          code: 200,
          msg: '请求成功',
        };
      }),
    );
  }
}
